package avicit.bdp.dds.server.worker.task;

import avicit.bdp.dds.dispatch.enums.ExecutionStatus;
import avicit.bdp.dds.dispatch.task.AbstractParameters;
import avicit.bdp.dds.common.Constants;
import avicit.bdp.dds.server.entity.TaskExecutionContext;
import org.slf4j.Logger;

import java.util.List;

/**
 * @author admin
 * executive task
 */
public abstract class AbstractTask {

    /**
     * taskExecutionContext
     **/
    TaskExecutionContext taskExecutionContext;

    /**
     * log record
     */
    protected Logger logger;


    /**
     * SHELL process pid
     */
    protected Integer processId;

    /**
     * other resource manager appId , for example : YARN etc
     */
    protected String appIds;


    /**
     * cancel
     */
    protected volatile boolean cancel = false;

    /**
     * exit code
     */
    protected volatile int exitStatusCode = -1;

    private static final String V_PROC_DATE = "v_proc_date";

    /**
     * constructor
     *
     * @param taskExecutionContext taskExecutionContext
     * @param logger               logger
     */
    protected AbstractTask(TaskExecutionContext taskExecutionContext, Logger logger) {
        this.taskExecutionContext = taskExecutionContext;
        this.logger = logger;
    }

    /**
     * init task
     *
     * @throws Exception exception
     */
    public void init() throws Exception {
    }

    /**
     * task handle
     *
     * @throws Exception exception
     */
    public abstract void handle() throws Exception;


    /**
     * cancel application
     *
     * @param status status
     * @throws Exception exception
     */
    public void cancelApplication(boolean status) throws Exception {
        this.cancel = status;
    }

    /**
     * log handle
     *
     * @param logs log list
     */
    public void logHandle(List<String> logs) {
        // note that the "new line" is added here to facilitate log parsing
        logger.info(" -> {}", String.join("\n\t", logs));
    }

    /**
     * get exit status code
     *
     * @return exit status code
     */
    public int getExitStatusCode() {
        return exitStatusCode;
    }

    public void setExitStatusCode(int exitStatusCode) {
        this.exitStatusCode = exitStatusCode;
    }

    public String getAppIds() {
        return appIds;
    }

    public void setAppIds(String appIds) {
        this.appIds = appIds;
    }

    public Integer getProcessId() {
        return processId;
    }

    public void setProcessId(Integer processId) {
        this.processId = processId;
    }

    /**
     * get task parameters
     *
     * @return AbstractParameters
     */
    public abstract AbstractParameters getParameters();

    /**
     * result processing
     */
    public void after() {
        logger.info("after process start,exit status code: {}", getExitStatusCode());
        if (getExitStatusCode() == Constants.EXIT_CODE_SUCCESS) {
            // task record flag : if true , start up qianfan
            setExitStatusCode(Constants.EXIT_CODE_SUCCESS);
        } else if (getExitStatusCode() == Constants.EXIT_CODE_KILL) {
            setExitStatusCode(Constants.EXIT_CODE_KILL);
        } else {
            setExitStatusCode(Constants.EXIT_CODE_FAILURE);
        }

        logger.info("after process end , task exit status code : {}", getExitStatusCode());
    }

    /**
     * get exit status according to exitCode
     *
     * @return exit status
     */
    public ExecutionStatus getExitStatus() {
        ExecutionStatus status;
        switch (getExitStatusCode()) {
            case Constants.EXIT_CODE_SUCCESS:
                status = ExecutionStatus.SUCCESS;
                break;
            case Constants.EXIT_CODE_KILL:
                status = ExecutionStatus.KILL;
                break;
            default:
                status = ExecutionStatus.FAILURE;
                break;
        }
        return status;
    }

}
